//
//  ViewController.swift
//  CALayerMask
//
//  Created by liy on 2025/1/16.
//
//https://www.cnblogs.com/YouXianMing/p/3788270.html
import UIKit

class ViewController: UIViewController {
  var imageLayer = CALayer()
  var maskLayer = CALayer()

  let imageContents = UIImage(named: "sky")!
  let maskContents = UIImage(named: "mask")!

  override func viewDidLoad() {
    super.viewDidLoad()

    // 1.创建layer，设置contents属性， 加到view layer上
    imageLayer.frame = view.bounds
    imageLayer.contents = imageContents.cgImage
    imageLayer.contentsGravity = CALayerContentsGravity.resizeAspectFill // 图片显示模式fill

    view.layer.addSublayer(imageLayer)

    // 2.创建遮罩maskLayer 并设置为imageLayer的mask属性
    maskLayer.frame = CGRectMake(50, 150, 200, 200)
    maskLayer.contents = maskContents.cgImage
    imageLayer.mask = maskLayer
    // 圆角
    maskLayer.cornerRadius = 100
    maskLayer.masksToBounds = true
		
		// 3.加个动画
		perform(#selector(maskAnimation), with: nil, afterDelay: 2)
  }
	@objc func maskAnimation(){
		maskLayer.cornerRadius = 50
		maskLayer.frame = CGRectMake(50, 300, 100, 100)
	}
}
